package com.samsung.svmc.moneymanager.data;

import java.util.ArrayList;
import com.samsung.svmc.moneymanager.object.TheLoaiTaiKhoan;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbTheLoaiTaiKhoan extends DbConnect {
	
	protected String tag = "SQLConnection";
	protected static DbTheLoaiTaiKhoan instance;
	protected Context context;
	protected DbHelper _help;
	public static final String TABLE = "tbl_theloai_tk";
	public static final String Id = "id";
	public static final String TenTheLoai = "ten";
	
	public DbTheLoaiTaiKhoan(Context c){
		context = c;
	}
	
	public DbTheLoaiTaiKhoan getInstance(Context c){
		if(instance == null)
			instance = new DbTheLoaiTaiKhoan(c);
		return instance;
	}
	
	public DbTheLoaiTaiKhoan open() throws SQLException{
		_help = new DbHelper(context);
		_Db = _help.getWritableDatabase();
		return this;
	}
	
	public void close(){
		_help.close();
	}
	
	public void insert(TheLoaiTaiKhoan t){
		ContentValues cv = new ContentValues();
		cv.put(TenTheLoai, t.getTenTheLoai());		
		_Db.insert(TABLE, null, cv);
	}
	
	public void update(TheLoaiTaiKhoan t){
		ContentValues cv = new ContentValues();
		cv.put(TenTheLoai, t.getTenTheLoai());		
		_Db.update(TABLE, cv,Id+"="+t.getId(), null);
	}
	
	public void delete(int index){
		_Db.delete(TABLE, Id+"="+index, null);
	}
	
	public TheLoaiTaiKhoan getTheLoai(int index){
		TheLoaiTaiKhoan tmp = new TheLoaiTaiKhoan();
		Cursor c = _Db.query(TABLE, null, Id+"=?", new String[]{index+""}, null, null, null);
		if(c == null)
			Log.d(tag, "table the loai tai khoan has not data");
		int iId = c.getColumnIndex(Id);
		int iTen = c.getColumnIndex(TenTheLoai);		
		while(c.moveToNext()){
			tmp.setId(c.getInt(iId));			
			tmp.setTenTheLoai(c.getString(iTen));			
			break;
		}
		c.close();
		return tmp;
	}
	
	public ArrayList<TheLoaiTaiKhoan> getData(){
		ArrayList<TheLoaiTaiKhoan> lv = new ArrayList<TheLoaiTaiKhoan>();
		Cursor c = _Db.query(TABLE, null, null, null, null, null, null);
		if(c == null)
			Log.d(tag, "table the loai tai khoan has not data");
		int iId = c.getColumnIndex(Id);
		int iTen = c.getColumnIndex(TenTheLoai);
		while(c.moveToNext()){
			TheLoaiTaiKhoan tmp = new TheLoaiTaiKhoan();
			tmp.setId(c.getInt(iId));			
			tmp.setTenTheLoai(c.getString(iTen));
			lv.add(tmp);
		}
		c.close();
		return lv;
	}
	
private static class DbHelper extends SQLiteOpenHelper {
		
		public DbHelper(Context c){
			super(c,DATABASE_NAME , null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			String sql = "CREATE TABLE " + TABLE + " ("
					+ Id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
					+ TenTheLoai + " TEXT "					
			+ ");";
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			String sql = " DROP TABLE IF EXISTS " + TABLE;
			db.execSQL(sql);
			onCreate(db);
		}
		
	}

}
